<%@page import="java.util.Collection"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueListMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueList"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueDate"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueNumberMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueTextMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueText"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%-- imports --%>
<%@page import="com.dilean.impl.model.PersonImpl"%>
<%@page import="com.dilean.api.model.Person"%>
<%@page import="com.dilean.impl.dao.PersonDAOImpl"%>
<%@page import="com.dilean.api.dao.PersonDAO"%>
<%@page import="com.dilean.spring.AplicationContextHolder"%>
<%@page import="java.sql.*"%>
<!DOCTYPE html>
<html lang="en">
    <meta charset="utf-8">
    <title>Action &middot; Dilean</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
    <!-- CSS and other resourses -->
    <link href="resources/mytheme/css/bootstrap.min.css" rel="stylesheet">
    <link href="resources/mytheme/css/simple-sidebar.css" rel="stylesheet">
    <link href="resources/mytheme/css/font-awesome.css" rel="stylesheet">
    <link href="resources/mytheme/css/font-awesome.min.css" rel="stylesheet">
    <script src="resources/mytheme/js/jquery-1.11.0.js"></script>
    <script src="resources/mytheme/js/bootstrap.min.js"></script>
    <style>
        body {
            margin-top: 20px;
        }
    </style>
    <body>
        <%
            int myId = Integer.parseInt(request.getParameter("id"));
            int friendId = Integer.parseInt(request.getParameter("frid"));
            int action = Integer.parseInt(request.getParameter("par"));
            Person me = new PersonImpl();
            Person friend = new PersonImpl();
            me = AplicationContextHolder.getInstance().getContext().getBean(PersonDAO.class).findByIdWithFriends(myId);
            friend = AplicationContextHolder.getInstance().getContext().getBean(PersonDAO.class).findByIdWithFriends(friendId);
            String fullname = "User #" + friendId;
            AttrValueText firstNameUser = friend.getAttributeValue(1, AttrValueText.class);
            AttrValueText lastNameUser = friend.getAttributeValue(2, AttrValueText.class);
            if ((firstNameUser != null) && (lastNameUser != null)) {
                fullname = firstNameUser.getValue() + " " + lastNameUser.getValue();
            } else if ((firstNameUser == null) && (lastNameUser != null)) {
                fullname = lastNameUser.getValue();
            } else if ((firstNameUser != null) && (lastNameUser == null)) {
                fullname = firstNameUser.getValue();
            }
            Collection<Person> friendRequests = me.getFriendRequests();
            Collection<Person> friends = me.getFriends();
            int numberRequest = 0;
            for (Person fr : friendRequests) {
                if (friends.contains(fr) == false) {
                    numberRequest++;
                }
            }
        %>
        <div id="wrapper">
            <!-- Sidebar -->
            <div id="sidebar-wrapper">
                <ul class="sidebar-nav">
                    <li class="sidebar-brand">
                        <a href="http://localhost:8080/Dilean/myPage.jsp">
                            <h2 align="centre" >Dilean</h2>
                        </a>
                    </li>
                    <li>
                        <a href="http://localhost:8080/Dilean/myPage.jsp"><i class="fa fa-home fa-fw-5x"></i>&nbsp; My Page</a>
                    </li>
                    <li>
                        <a href="http://localhost:8080/Dilean/friends.jsp?id=1"><i class="fa fa-users"></i>&nbsp; Friends </a>
                    </li>

                    <li>
                        <a href="http://localhost:8080/Dilean/allUsers.jsp?id=1"><i class="fa fa-database"></i>&nbsp; Users 
                            <%
                                if (numberRequest != 0) {
                            %>
                            <span class="badge badge-info"><%=numberRequest%> </span>
                            <%
                                }
                            %>
                        </a>
                    </li>
                    <li>
                        <a href="http://localhost:8080/Dilean/conversation.jsp"><i class="fa fa-envelope"></i>&nbsp; Messages </a>
                    </li>
                    <li class="divider"></li>
                    <li>
                        <a href="#"><i class="fa fa-cog"></i>&nbsp; Settings</a>
                    </li>
                    <li>
                        <a href="#"><i class="fa fa-book"></i>&nbsp; About</a>
                    </li>
                    <li>
                        <a href="#"><i class="fa fa-sign-out"></i>&nbsp; LogOut</a>
                    </li>
                </ul>
            </div>
            <!-- /#sidebar-wrapper -->
            <!-- Page Content -->
            <div id="page-content-wrapper">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-lg-12">
                            <a class="btn btn-large btn-primary" href="#menu-toggle" id="menu-toggle" >
                                <i class="fa fa-user fa-3x"></i> <h4>User Menu</h4></a>
                            <!-- /Main menu -->
                            <!-- user Profile -->

                            <div class="container">
                                <div class="row">
                                    <div class="well col-xs-10 col-sm-10 col-md-6 col-xs-offset-1 col-sm-offset-1 col-md-offset-3">
                                        <div class="row">
                                            <div class="col-xs-6 col-sm-6 col-md-6">
                                            </div>
                                            <div class="col-xs-6 col-sm-6 col-md-6 text-right">
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="text-center">
                                                <h1>Success</h1>
                                                <%
                                                    if (action == 1) {
                                                %>
                                                <%                            } else if (action == 2) {
                                                    me.addToFriends(friend);
                                                    AplicationContextHolder.getInstance().getContext().getBean(PersonDAO.class).save(me);
                                                %>
                                                <h2>You Accept <a href="http://localhost:8080/Dilean/friend.jsp?id=<%=friendId%>"><%=fullname%></a> Friend Request</h2>
                                                <%
                                                } else if (action == 3) {
                                                    me.addToFriends(friend);
                                                    AplicationContextHolder.getInstance().getContext().getBean(PersonDAO.class).save(me);
                                                %>
                                                <h2>You sent friend request to <a href="http://localhost:8080/Dilean/friend.jsp?id=<%=friendId%>"><%=fullname%></a></h2>   
                                                    <%                } else if (action == 4) {
                                                        me.deliteFriend(friend);
                                                        AplicationContextHolder.getInstance().getContext().getBean(PersonDAO.class).save(me);
                                                    %>
                                                <h2>You delite <a href="http://localhost:8080/Dilean/friend.jsp?id=<%=friendId%>"><%=fullname%></a> from your friends</h2>
                                                <%                }
                                                %>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>           
                        </div>
                        <script>
                            $("#menu-toggle").click(function(e) {
                                e.preventDefault();
                                $("#wrapper").toggleClass("toggled");
                            });
                        </script>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>

